package worldinruin.core.imp;

import java.awt.Color;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.LinkedList;
import java.util.List;

import worldinruin.core.IBuilding;
import worldinruin.core.IPlayer;
import worldinruin.core.IResources;
import worldinruin.resources.Events;

public class Player implements IPlayer {
	
	private final String name;
	private final Color color;
	private IResources resources;
	private PropertyChangeSupport propertyChangeSupport;
	private List<IBuilding> buildings = new LinkedList<IBuilding>();
	
	/**
	 * Creates a player with the specified name.
	 * @param name Name of the player.
	 */
	public Player(String name) {
		this(name, new Color((int)(Math.random() * 255), (int)(Math.random() * 255), (int)(Math.random() * 255)));
	}
		
	/**
	 * Creates a player with the specified name and color.
	 * @param name Name of the player.
	 * @param color Player's color.
	 */
	public Player(String name, Color color) {
		this(name, color, new Resources(100, 100, 100, 100));
	}
	
	/**
	 * Creates a player with the specified name, color and starting resources.
	 * @param name Name of the player.
	 * @param color Player's color.
	 * @param resources Starting resources.
	 */
	public Player(String name, Color color, IResources resources) {
		this.name = name;
		this.color = color;
		this.resources = resources;
		this.propertyChangeSupport = new PropertyChangeSupport(this);
	}
	
	/* (non-Javadoc)
	 * @see worldinruin.core.imp.IPlayer#getName()
	 */
	public String getName() {
		return name;
	}
	/* (non-Javadoc)
	 * @see worldinruin.core.imp.IPlayer#getColor()
	 */
	public Color getColor() {
		return color;
	}	
	/* (non-Javadoc)
	 * @see worldinruin.core.imp.IPlayer#getResources()
	 */
	public IResources getResources(){
		return resources;
	}

	/* (non-Javadoc)
	 * @see worldinruin.core.imp.IPlayer#getBuildings()
	 */
	public List<IBuilding> getBuildings() {
		return buildings;
	}

	/* (non-Javadoc)
	 * @see worldinruin.core.imp.IPlayer#addBuilding(worldinruin.core.IBuilding)
	 */
	public void addBuilding(IBuilding building){
		List<IBuilding> oldValue = buildings;
		buildings.add(building);
		propertyChangeSupport.firePropertyChange(Events.buildingAdded, oldValue, buildings);
	}
	
	/* (non-Javadoc)
	 * @see worldinruin.core.imp.IPlayer#removeBuilding(worldinruin.core.IBuilding)
	 */
	public void removeBuilding(IBuilding building){
		List<IBuilding> oldValue = buildings;
		buildings.remove(building);
		propertyChangeSupport.firePropertyChange(Events.buildingRemoved, oldValue, buildings);
	}

	/* (non-Javadoc)
	 * @see worldinruin.core.imp.IPlayer#canAfford(worldinruin.core.Resources)
	 */
	public boolean canAfford(IResources r){
		return resources.isAffordable(r);
	}
	
	/* (non-Javadoc)
	 * @see worldinruin.core.imp.IPlayer#addToResources(worldinruin.core.Resources)
	 */
	@Override
	public void addToResources(IResources r){
		IResources oldValue = resources;
		
		resources = resources.add(r);
		
		propertyChangeSupport.firePropertyChange("added to resources", oldValue, resources);
	}
	
	/* (non-Javadoc)
	 * @see worldinruin.core.imp.IPlayer#subtractFromResources(worldinruin.core.Resources)
	 */
	@Override
	public void subtractFromResources(IResources r){

		IResources oldValue = resources;
		
		resources = resources.subtract(r);
		propertyChangeSupport.firePropertyChange("subtracted from resources", oldValue, resources);

	}

	/* (non-Javadoc)
	 * @see worldinruin.core.imp.IPlayer#addPropertyChangeListener(java.beans.PropertyChangeListener)
	 */
	public void addPropertyChangeListener(PropertyChangeListener l){
		propertyChangeSupport.addPropertyChangeListener(l);
	}
	
	/* (non-Javadoc)
	 * @see worldinruin.core.imp.IPlayer#removePropertyChangeListener(java.beans.PropertyChangeListener)
	 */
	public void removePropertyChangeListener(PropertyChangeListener l){
		propertyChangeSupport.removePropertyChangeListener(l);
	}

}
